package com.sskj.api.handler.serializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.sskj.common.util.StrUtil;
import java.io.IOException;

public class WithDomainSerializer extends JsonSerializer<String > {

    private final String domain;

    public WithDomainSerializer(String domain) {
        this.domain = domain;
    }

    @Override
    public void serialize(String s, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        if (StrUtil.isBlank(s)) {
            jsonGenerator.writeNull();
            return;
        }
        String fillUrl = (s.startsWith("http")) ? s : domain + s;
        jsonGenerator.writeString(fillUrl);
    }
}
